package com.yuyou.fn.electronicinvoice.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.github.wens.mybatisplus.toolkit.IdWorker;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.*;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.electronicinvoice.entity.FinanceSubject;
import com.yuyou.fn.electronicinvoice.entity.FinanceSubjectCampus;
import com.yuyou.fn.electronicinvoice.mapper.FinanceSubjectMapper;
import com.yuyou.fn.electronicinvoice.service.IFinanceSubjectCampusService;
import com.yuyou.fn.electronicinvoice.service.IFinanceSubjectService;
import com.yuyou.fn.electronicinvoice.vo.FinanceSubjectQueryVo;
import com.yuyou.fn.electronicinvoice.vo.FinanceSubjectSaveVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * FinanceSubject 表数据服务层接口实现类
 *
 */
@Service
public class FinanceSubjectServiceImpl extends SuperServiceImpl<FinanceSubjectMapper, FinanceSubject,Long> implements IFinanceSubjectService {

    @Resource
    private IFinanceSubjectCampusService financeSubjectCampusService;

    @Resource
    private ICampusService campusService;

    @Override
    @Transactional
    public void saveOrUpdate(FinanceSubjectSaveVo financeSubjectSaveVo) {
        FinanceSubject financeSubject=new FinanceSubject();
        BeanUtils.copyProperties(financeSubject,financeSubjectSaveVo);
        financeSubject.setUpdateTime(TimeUtils.now());
        Principals.Principal principal=Principals.get();
        financeSubject.setOperatorId(principal.id);
        financeSubject.setOperatorName(principal.name);
        List<Long> campusIds= Utils.toLongIds(financeSubjectSaveVo.getCampusIds());
        Boolean update=true;
        if (financeSubjectSaveVo.getFinanceSubjectId()==null){
            update=false;
            financeSubject.setFinanceSubjectId(IdWorker.getId());
        }else {
            financeSubjectCampusService.deleteByFinanceSubjectId(financeSubjectSaveVo.getFinanceSubjectId());
        }
        if (campusIds!=null && campusIds.size()>0){
            List<String> changeCampusNames=new ArrayList<>(campusIds.size());
            List<Long> changeCampusIds=new ArrayList<>(campusIds.size());
            if (financeSubject.getSubjectStatus()==1){
                this.checkCampus(campusIds);
            }
            List<Campus> campusList=campusService.findByIds(campusIds);
            List<FinanceSubjectCampus> saveList=new ArrayList<>(campusList.size());
            for (Campus campus:campusList){
                FinanceSubjectCampus financeSubjectCampus=new FinanceSubjectCampus();
                financeSubjectCampus.setUpdateTime(TimeUtils.now());
                financeSubjectCampus.setCreateTime(TimeUtils.now());
                financeSubjectCampus.setCampusId(campus.getCampusId());
                financeSubjectCampus.setCampusName(campus.getCampusName());
                financeSubjectCampus.setFinanceSubjectId(financeSubject.getFinanceSubjectId());
                financeSubjectCampus.setFinanceSubjectName(financeSubject.getFinanceSubjectName());
                financeSubjectCampus.setSubjectStatus(financeSubject.getSubjectStatus());
                saveList.add(financeSubjectCampus);
                changeCampusNames.add(campus.getCampusName());
                changeCampusIds.add(campus.getCampusId());

            }
            financeSubject.setCampusIds(StringUtils.join(changeCampusIds,","));
            financeSubject.setCampusNames(StringUtils.join(changeCampusNames,","));
            financeSubjectCampusService.insertBatch(saveList);
        }

        if (update){
            this.updateById(financeSubject);
        }else {
            financeSubject.setCreateTime(TimeUtils.now());
            this.insert(financeSubject);
        }
    }

    @Override
    @Transactional
    public void changeSubjectStatus(Long financeSubjectId, Integer subjectStatus) {
        FinanceSubject financeSubject=this.findById(financeSubjectId);
        if (subjectStatus==1 && financeSubject.getSubjectStatus()==0){
            List<Long> campusIds=Utils.toLongIds(financeSubject.getCampusIds());
            this.checkCampus(campusIds);
        }
        FinanceSubject update=new FinanceSubject();
        update.setFinanceSubjectId(financeSubjectId);
        update.setUpdateTime(TimeUtils.now());
        update.setSubjectStatus(subjectStatus);
        this.updateSelectiveById(update);
        if (StringUtils.isNotEmpty(financeSubject.getCampusIds())){
            financeSubjectCampusService.changeBySubjectIdAndSubjectStatus(financeSubjectId,subjectStatus);
        }
    }


    @Override
    public void checkCampus(List<Long> campusIds) {
        List<FinanceSubjectCampus> financeSubjectCampusList=financeSubjectCampusService.findByCampusIds(campusIds,1);
        if (financeSubjectCampusList!=null && financeSubjectCampusList.size()>0){
            List<String> campusNameList=new ArrayList<>(financeSubjectCampusList.size());
            //List<String> financeNameList=new ArrayList<>(financeSubjectCampusList.size());
            for (FinanceSubjectCampus financeSubjectCampus:financeSubjectCampusList){
                campusNameList.add(financeSubjectCampus.getCampusName());
              //  financeNameList.add(financeSubjectCampus.getFinanceSubjectName());
            }
            String campusNames= StringUtils.join(campusNameList,",");
            //String financeNames=StringUtils.join(financeNameList,",");
            throw new BusException("以下校区已设置财务主体，无法重复设置！【"+campusNames+"】");
        }
    }


    @Override
    public Page<FinanceSubject> findByQueryVo(FinanceSubjectQueryVo financeSubjectQueryVo,int pageNo,int pageSize) {
        Example<FinanceSubject> example=FinanceSubject.createExample();
        Example.Criteria criteria=example.createCriteria();
        CriteriaUtils.fillCondition(criteria,financeSubjectQueryVo);
        example.orderBy("createTime").desc();
        return this.findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public FinanceSubject findByCampusIdAndSubjectStatus(String campusId, Integer subjectStatus) {
        Example<FinanceSubject> example=FinanceSubject.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andLike("campusIds","%"+campusId+"%");
        criteria.andEqualTo("subjectStatus",subjectStatus);
        return this.findOneByExample(example);
    }
}